package cn.tedu.crm.role.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.tedu.crm.base.pojo.PageObject;
import cn.tedu.crm.base.response.JsonResult;
import cn.tedu.crm.role.pojo.dto.RoleQuery;
import cn.tedu.crm.role.pojo.dto.RoleSaveParam;
import cn.tedu.crm.role.pojo.vo.RoleVO;
import cn.tedu.crm.role.pojo.vo.RoleSelectVO;
import cn.tedu.crm.role.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/sys/roles")
public class RoleController {
    @Autowired
    RoleService roleService;

    @GetMapping("/select")
    public JsonResult selectRole(RoleQuery roleQuery) {
        log.debug("查询角色：{}", roleQuery);
        PageObject<RoleVO> pageObject = PageObject.page(
                roleQuery.getPageNum(), roleQuery.getPageSize(),
                ()->roleService.selectRole(roleQuery));
        log.debug(":{}", pageObject);
        return JsonResult.ok(pageObject);
    }

    @SaCheckPermission("crm:system:role:del")
    @DeleteMapping("/delete")
    public JsonResult deleteRole( Long ids) {
        log.debug("删除角色：{}",ids);
        roleService.deleteRole(ids);
        return JsonResult.ok();
    }

    @SaCheckPermission("crm:system:role:add")
    @PostMapping ("/save")
    public JsonResult saveRole(@RequestBody RoleSaveParam roleSaveParam) {
        log.debug("新增角色：{}",roleSaveParam);
        roleService.saveRole(roleSaveParam);
        return JsonResult.ok();
    }

    @SaCheckPermission("crm:system:role:updata")
    @GetMapping("/updata/select/{id}")
    public JsonResult selectRoleById(@PathVariable Long id){
        log.debug("修改角色id：{}",id);
        RoleSelectVO roleSelectVO = roleService.selectRoleById(id);
        return JsonResult.ok(roleSelectVO);
    }


    @GetMapping("/all")
    public JsonResult<List<RoleVO>> selectAllRole(){
        return JsonResult.success(roleService.selectAllRole());
    }

}
